package com.jet.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jet.core.model.vo.PageVo;
import com.jet.core.utils.IdUtil;
import com.jet.member.domain.dto.MemberAssetsLogDto;
import com.jet.member.domain.entity.MemberAssetsLog;
import com.jet.member.domain.query.MemberAssetsLogQuery;
import com.jet.member.domain.vo.MemberAssetsLogVo;
import com.jet.member.domain.vo.MemberInfoVo;
import com.jet.member.mapper.MemberAssetsLogMapper;
import com.jet.member.service.IMemberAssetsLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jet.member.service.IMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-02
 */
@Service
public class MemberAssetsLogServiceImpl extends ServiceImpl<MemberAssetsLogMapper, MemberAssetsLog> implements IMemberAssetsLogService {

    private final IMemberService memberService;

    public MemberAssetsLogServiceImpl(IMemberService memberService) {
        this.memberService = memberService;
    }

    @Override
    public void create(MemberAssetsLogDto dto) {
        MemberAssetsLog entity = new MemberAssetsLog();
        entity.setId(IdUtil.generateLower());
        entity.setMemberId(dto.getMemberId());
        entity.setOrderNo(dto.getOrderNo());
        entity.setType(dto.getType());
        entity.setCategory(dto.getCategory());
        entity.setBeforeAssets(dto.getBeforeAssets());
        entity.setChangeAssets(dto.getChangeAssets());
        entity.setAfterAssets(dto.getAfterAssets());
        entity.setRemark(dto.getRemark());
        super.save(entity);
    }

    @Override
    public List<MemberAssetsLog> findAllByOrderNo(String memberId, String orderNo) {
        Wrapper<MemberAssetsLog> wrapper = new LambdaQueryWrapper<MemberAssetsLog>()
                .eq(MemberAssetsLog::getMemberId, memberId)
                .eq(MemberAssetsLog::getOrderNo, orderNo);
        return super.list(wrapper);
    }

    @Override
    public PageVo<MemberAssetsLogVo> page(MemberAssetsLogQuery query) {
        if (query.getStartDate() == null) {
            query.setStartDate(LocalDate.now());
        }
        if (query.getEndDate() == null) {
            query.setEndDate(LocalDate.now());
        }
        query.setEndDate(query.getEndDate().plusDays(1));
        Page<MemberAssetsLog> tempQuery = new Page<>(query.getPageIndex(), query.getPageSize());
        Wrapper<MemberAssetsLog> wrapper = new LambdaQueryWrapper<MemberAssetsLog>()
                .eq(MemberAssetsLog::getMemberId, query.getMemberId())
                .eq(MemberAssetsLog::getType, query.getType())
                .ge(MemberAssetsLog::getCreateTime, query.getStartDate())
                .lt(MemberAssetsLog::getCreateTime, query.getEndDate())
                .orderByDesc(MemberAssetsLog::getCreateTime);
        IPage<MemberAssetsLog> page = super.page(tempQuery, wrapper);

        List<MemberAssetsLogVo> vos = page.getRecords().stream().map(x -> {
            MemberAssetsLogVo logVo = new MemberAssetsLogVo();
            BeanUtils.copyProperties(x, logVo);
            MemberInfoVo member = memberService.findById(x.getMemberId());
            if (member != null) {
                logVo.setMemberName(member.getName());
            }
            return logVo;
        }).collect(Collectors.toList());
        return PageVo.result(page, vos);
    }
}
